package com.ruoyi.base.service.impl;

import java.util.List;

import com.ruoyi.base.domain.BaseFruitMove;
import com.ruoyi.base.mapper.BaseFruitMoveMapper;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.base.mapper.BaseFruitStockMapper;
import com.ruoyi.base.domain.BaseFruitStock;
import com.ruoyi.base.service.IBaseFruitStockService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 果品库存操作Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-21
 */
@Service
public class BaseFruitStockServiceImpl implements IBaseFruitStockService {
    @Autowired
    private BaseFruitStockMapper baseFruitStockMapper;

    @Autowired
    private BaseFruitMoveMapper baseFruitMoveMapper;
    /**
     * 查询果品库存操作
     *
     * @param id 果品库存操作主键
     * @return 果品库存操作
     */
    @Override
    public BaseFruitStock selectBaseFruitStockById(Long id) {
        return baseFruitStockMapper.selectBaseFruitStockById(id);
    }

    /**
     * 查询果品库存操作列表
     *
     * @param baseFruitStock 果品库存操作
     * @return 果品库存操作
     */
    @Override
    public List<BaseFruitStock> selectBaseFruitStockList(BaseFruitStock baseFruitStock) {
        return baseFruitStockMapper.selectBaseFruitStockList(baseFruitStock);
    }

    /**
     * 新增果品库存操作
     *
     * @param baseFruitStock 果品库存操作
     * @return 结果
     */
    @Override
    public int insertBaseFruitStock(BaseFruitStock baseFruitStock) {
        return baseFruitStockMapper.insertBaseFruitStock(baseFruitStock);
    }

    /**
     * 修改果品库存操作
     *
     * @param baseFruitStock 果品库存操作
     * @return 结果
     */
    @Override
    @Transactional
    public int updateBaseFruitStock(BaseFruitStock baseFruitStock) {
        BaseFruitMove baseFruitMove = new BaseFruitMove();
        if (baseFruitStock.getFruitMoveStatus() == 0) {
            baseFruitStock.setWeight(baseFruitStock.getWeight() - baseFruitStock.getMoveWeight());
            BeanUtils.copyProperties(baseFruitStock, baseFruitMove);
        } else {
            baseFruitStock.setWeight(baseFruitStock.getWeight() + baseFruitStock.getMoveWeight());
            BeanUtils.copyProperties(baseFruitStock, baseFruitMove);
        }
        baseFruitMove.setFruitMoveWeight(baseFruitStock.getMoveWeight());
        baseFruitMove.setFruitId(baseFruitStock.getId());
        baseFruitMove.setCreateTime(DateUtils.getNowDate());
        baseFruitMoveMapper.insertBaseFruitMove(baseFruitMove);
        return baseFruitStockMapper.updateBaseFruitStock(baseFruitStock);
    }

    /**
     * 批量删除果品库存操作
     *
     * @param ids 需要删除的果品库存操作主键
     * @return 结果
     */
    @Override
    public int deleteBaseFruitStockByIds(Long[] ids) {
        return baseFruitStockMapper.deleteBaseFruitStockByIds(ids);
    }

    /**
     * 删除果品库存操作信息
     *
     * @param id 果品库存操作主键
     * @return 结果
     */
    @Override
    public int deleteBaseFruitStockById(Long id) {
        return baseFruitStockMapper.deleteBaseFruitStockById(id);
    }
}
